//
//  KKXuanShangInfoVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/19.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKXuanShangInfoVC.h"
#import "KKJoinXuanShangVC.h"
#import "KKPublishXuanShangVC.h"
#import "KKComHttp.h"
#import "KKQuestionDetailM.h"
#import "KKShareDialog.h"
#import "KKShowImgCell.h"
#import "KKShowArticleCell.h"
#import "KKTxtContentM.h"
#import "KKImgSectionCell.h"
#import "KKArticleSectionCell.h"
#import "KKPersonalHomeVC.h"
#import "KKMyHttp.h"

@interface KKXuanShangInfoVC ()<UITableViewDelegate,UITableViewDataSource>{
    int countTime;
}
@property (weak, nonatomic) IBOutlet UILabel *money;
@property (weak, nonatomic) IBOutlet UILabel *xuanShangTime;
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (weak, nonatomic) IBOutlet UILabel *title1;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UILabel *detailContent;
@property (weak, nonatomic) IBOutlet UIButton *adoptBtn;
@property (strong, nonatomic)KKQuestionDetailM *questionInfo;
@property (strong, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *questionDatas;
@property (weak, nonatomic) IBOutlet UIView *answerView;
@property (weak, nonatomic) IBOutlet UILabel *answerContent;
@property (weak, nonatomic) IBOutlet UIView *likeView;
@property (weak, nonatomic) IBOutlet UILabel *likeCount;
@property (weak, nonatomic) IBOutlet UIView *xuanShangView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xuanShangViewHeight;


@end

@implementation KKXuanShangInfoVC
-(NSMutableArray *)questionDatas{
    if(!_questionDatas){
        _questionDatas=[NSMutableArray array];
    }
    return _questionDatas;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=self.navTitle;
    
    //右侧分享
    UIButton *rightBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [rightBtn setImage:[UIImage imageNamed:@"sharebgblack"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    if([self.xuanShangMoney isEqualToString:@"0.00"]){
        self.adoptBtn.hidden=YES;
    }else{
        if([self.navTitle isEqualToString:@"悬赏详情"]){//不是回答内容
            self.adoptBtn.hidden=YES;
            self.answerView.hidden=YES;
        }else{
            self.tableView.hidden=YES;
            self.answerContent.text=self.answerCon;
            
            if([self.questionUid isEqualToString:UID]){
                self.adoptBtn.hidden=NO;
            }else{
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commitLike)];
                [self.likeView addGestureRecognizer:tap];
                self.likeView.hidden=NO;
                self.adoptBtn.hidden=YES;
            }
        }
    }

    //tableview
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKShowArticleCell" bundle:nil] forCellReuseIdentifier:@"KKShowArticleCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKShowImgCell" bundle:nil] forCellReuseIdentifier:@"KKShowImgCell"];
    
    self.likeCount.text=self.answerInfo.likecoun;
    [self getQuestionDetail];
    [self getQuestionContentList];
}

-(void)scheduleTime{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if(countTime>0){
            self.xuanShangTime.text=[NSString stringWithFormat:@"距悬赏结束还有%@",[KKAppUtils timeformatFromSeconds:countTime]];
            countTime--;
        }
        else{
            [self.timer invalidate];
            self.xuanShangTime.text=[NSString stringWithFormat:@"悬赏已经结束"];
        }
        
    }];
}

-(void)initData{
    if([self.navTitle isEqualToString:@"回答详情"]){
        [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.answerInfo.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAvatar)];
        [self.avatar addGestureRecognizer:tap];
        if([KKAppUtils isEmptyString:self.answerInfo.nickname]){
            self.name.text=@"匿名";
        }else{
            self.name.text=self.answerInfo.nickname;
        }
        self.job.text=self.answerInfo.job;
        self.title1.text=self.answerInfo.respondcontent;
        self.content.text=@"";
        self.detailContent.text=self.answerInfo.respondcontent;
        self.label.text=@"";
        self.date.text=self.answerInfo.respondtime;

    }else{
        [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.questionInfo.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAvatar)];
        [self.avatar addGestureRecognizer:tap];
        if([KKAppUtils isEmptyString:self.questionInfo.nickname]){
            self.name.text=@"匿名";
        }else{
            self.name.text=self.questionInfo.nickname;
        }
        self.job.text=self.questionInfo.job;
        self.label.text=self.questionInfo.label;
        self.title1.text=self.questionInfo.title;
        self.content.text=self.questionInfo.questiondesc;
        self.detailContent.text=self.questionInfo.questiondesc;
        self.date.text=self.questionInfo.systime;
    }
    
    if([self.questionInfo.money isEqualToString:@"0.00"]){
        self.xuanShangViewHeight.constant=0;
        self.xuanShangView.hidden=YES;
    }else{
        self.xuanShangViewHeight.constant=50;
        self.xuanShangView.hidden=NO;
        //0未开始 1已开始 2 已结束
        if(self.questionInfo.status==0){
            self.xuanShangTime.text=@"悬赏尚未开始";
        }else if(self.questionInfo.status==1){
            countTime=[self.questionInfo.lefttime intValue];
            [self scheduleTime];
        }else{
            self.xuanShangTime.text=@"悬赏已经结束";
        }
        self.money.text=[NSString stringWithFormat:@"%.1f元",[self.questionInfo.money floatValue]];
    }
}
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.questionDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKTxtContentM *content=self.questionDatas[indexPath.row];
    if([content.contenttype isEqualToString:@"0"]){
        KKShowImgCell *cell=[tableView dequeueReusableCellWithIdentifier:@"KKShowImgCell"];
        [cell initShowData:content.content];
        return cell;
    }else{
        KKShowArticleCell *cell=[tableView dequeueReusableCellWithIdentifier:@"KKShowArticleCell"];
        [cell initData:content.content];
        return cell;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKTxtContentM *content=self.questionDatas[indexPath.row];
    if([content.contenttype isEqualToString:@"0"]){
        return screenWidth-20;
    }else{
        return content.contentDetailSize.height+10;
    }
}
#pragma mark 按钮--头像
-(void)pushAvatar{
    KKPersonalHomeVC *personHome=[[KKPersonalHomeVC alloc] init];
    
    if([self.navTitle isEqualToString:@"回答详情"]){
        personHome.expertId=self.publishId;
    }else if(@"悬赏详情"){
        personHome.expertId=self.publishId;
    }
    [self.navigationController pushViewController:personHome animated:YES];
}
    
#pragma mark 按钮--分享
-(void)share{
    KKShareDialog *share=[[[NSBundle mainBundle] loadNibNamed:@"KKShareDialog" owner:self options:nil] lastObject];
    share.shareContent=@"xxxxxxx";
    share.shareCallbackBlock=^(int flag){
        if(flag==0){
            [KKAppUtils toast:@"分享失败"];
        }else{
            
        }
    };
    [share show];
}
#pragma mark 按钮--采纳
- (IBAction)adoptClick:(id)sender {
    [self commitAdopt];
}
#pragma mark 按钮--参加悬赏
- (IBAction)joinClick:(id)sender {
    [KKMyHttp userCheck:^(id data) {
        if([UID isEqualToString:self.questionInfo.uid]){
            [self toast:@"不能回答自己发布的"];
            return;
        }
        if(![self.questionInfo.money isEqualToString:@"0.00"]){
            if(self.questionInfo.status==2){
                [KKAppUtils toast:@"悬赏已经结束"];
                return;
            }
        }
        [self getQuestionCheck];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 按钮--发布悬赏
- (IBAction)publishClick:(id)sender {
    KKPublishXuanShangVC *publish=[[KKPublishXuanShangVC alloc] init];
    [self.navigationController pushViewController:publish animated:YES];
}

#pragma mark 接口--获取悬赏详情
-(void)getQuestionDetail{
    [self showDialog];
    [KKComHttp getQuestionDetail:self.questionId success:^(id data) {
        self.questionInfo=data;
        [self initData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--采纳
-(void)commitAdopt{
    [KKComHttp questionAdopt:self.answerId success:^(id data) {
        [KKAppUtils toast:@"采纳成功"];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--点赞
-(void)commitLike{
    [KKComHttp questionLike:self.answerId success:^(id data) {
        [KKAppUtils toast:@"点赞成功"];
        self.likeCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--文章详情
-(void)getQuestionContentList{
    [KKComHttp getQuestionContentList:self.questionId success:^(id data) {
        [self.questionDatas removeAllObjects];
        NSArray *list=[data objectForKey:@"listcontent"];
        NSArray *datas=[KKTxtContentM mj_objectArrayWithKeyValuesArray:list];
        [self.questionDatas addObjectsFromArray:datas];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--悬赏权限检测
-(void)getQuestionCheck{
    [KKComHttp questionCheck:self.questionId success:^(id data) {
        KKJoinXuanShangVC *join=[[KKJoinXuanShangVC alloc] init];
        join.questionId=self.questionId;
        [self.navigationController pushViewController:join animated:YES];
    } failure:^(NSError *error) {
        
    }];
}
@end
